package com.ruoyi.common.utils.pdf;

import cn.hutool.core.io.FileUtil;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

public class PDFUtiles {

    /**
     * 给PDF文档添加水印
     *
     * @param pdfFilePath    源文件地址
     * @param outputFilePath 输出到地址
     * @param cont           水印内容
     */
    public static void addWaterMark(String pdfFilePath, String outputFilePath, String cont) {
        try {
            // 原PDF文件
            PdfReader reader = new PdfReader(pdfFilePath);
            // 输出的PDF文件内容
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFilePath));

            // 字体 来源于 itext-asian JAR包
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);

            PdfGState gs = new PdfGState();
            // 设置透明度
            gs.setFillOpacity(0.3f);
            gs.setStrokeOpacity(0.4f);

            int totalPage = reader.getNumberOfPages() + 1;
            for (int i = 1; i < totalPage; i++) {
                // 内容上层
//			PdfContentByte content = stamper.getOverContent(i);
                // 内容下层
                PdfContentByte content = stamper.getUnderContent(i);

                content.beginText();
                // 字体添加透明度
                content.setGState(gs);
                // 添加字体大小等
                content.setFontAndSize(baseFont, 50);
                // 添加范围
                content.setTextMatrix(10, 200);
                // 具体位置 内容 旋转多少度 共360度
                content.showTextAligned(Element.ALIGN_CENTER, cont, 300, 350, 300);
                content.showTextAligned(Element.ALIGN_TOP, cont, 100, 100, 5);
                content.showTextAligned(Element.ALIGN_BOTTOM, cont, 400, 400, 75);

                content.endText();
            }

            // 关闭
            stamper.close();
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 添加水印和二维码
     *
     * @param destPath   生成文件得地址
     * @param sourcePath 原PDF位置
     * @param qrPath     二维码地址
     * @param content    水印内容
     */
    public static void addWatermar2(String destPath, String sourcePath, String content) {
        BufferedOutputStream bos = null;
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            File file = new File(destPath);
            if (!file.exists()) {
                FileUtil.file(file);
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            reader = new PdfReader(sourcePath);

            stamper = new PdfStamper(reader, bos);
            int total = reader.getNumberOfPages();
            PdfContentByte waterMar;

            PdfGState gs = new PdfGState();
            long startTime = System.currentTimeMillis();
            //  log.info("PDF加图片水印>> start");
            for (int i = 1; i <= total; i++) {
                waterMar = stamper.getOverContent(i);
                // 设置图片透明度
                gs.setFillOpacity(0.8f);
                // 设置笔触字体不透明度
                gs.setStrokeOpacity(0.1f);
                waterMar.saveState();
                // 开始水印处理
                waterMar.beginText();
                // 设置水印颜色
                waterMar.setColorFill(BaseColor.GRAY);
                // 设置透明度
                waterMar.setGState(gs);
                // 设置水印字体参数及大小
                waterMar.setFontAndSize(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED), 15);
                waterMar.setTextMatrix(20, 100);
                // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
                com.itextpdf.text.Rectangle pageSize = reader.getPageSize(i);
                float height = pageSize.getHeight();
                float width = pageSize.getWidth();
                float hSize = height / 3;
                Random ran = new Random();
                int num = 0;
                int num2 = 0;
                for (int k = 10; k <= width; k += 30) {
                    num = (int) (Math.random() * 50);
                    num2 = (int) (Math.random() * 100);
                    //  waterMar.showTextAligned(Element.ALIGN_LEFT, content, k, hSize - num, num2);
                    waterMar.showTextAligned(num % 3, content, k += 10, (int) (Math.random() * hSize), (int) (Math.random() * width));
                /*    waterMar.showTextAligned(Element.ALIGN_CENTER, content, k, hSize * 3 - num, num2);
                    waterMar.showTextAligned(Element.ALIGN_RIGHT, content, k - 150, hSize - num, num2);
                    waterMar.showTextAligned(Element.ALIGN_LEFT, content, k - 150, hSize * 2 - num, num2);
                    waterMar.showTextAligned(Element.ALIGN_LEFT, content, k - 150, hSize * 3 - num, num2);*/
                }


         /*       Image image = Image.getInstance(qrPath);
                // 水印图片位置
                image.setAbsolutePosition(60, height - 150);
                // 边框固定
                image.scaleToFit(200, 200);
                // 设置等比缩放
                image.scalePercent(90);
                // 自定义大小
                image.scaleAbsolute(70, 70);
                // 附件加上水印图片
                waterMar.addImage(image);*/

                // 完成水印添加
                waterMar.endText();
                // stroke
                waterMar.stroke();
            }
            long endTime = System.currentTimeMillis();
        } catch (Exception e) {
        } finally {
            try {
                if (stamper != null) {
                    stamper.close();
                }
                if (reader != null) {
                    reader.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        addWaterMark("F:\\tmp\\12.pdf", "F:\\tmp\\121.pdf", "122222222222222222222222s");
        //  addWatermar2("F:\\tmp\\12222.pdf", "F:\\tmp\\12222.pdf", "这是水印 机密文件");
    }
}
